CREATE
OR REPLACE PROCEDURE project_auto_to_ck (p_phid NUMBER) AS
    v_date_str   VARCHAR2(8);  -- 日期部分 YYYYMMDD
    v_seq        NUMBER(4);    -- 流水号
    p_bill_no VARCHAR2(50);
    per NUMBER;
    insert_phid NUMBER;

    req    UTL_HTTP.REQ;      -- 请求对象
    resp   UTL_HTTP.RESP;     -- 响应对象
    v_text CLOB := '';        -- 保存响应 JSON
    v_line VARCHAR2(4000);    -- 临时读取每行
 BEGIN
    -- 获取当天日期字符串
    v_date_str := TO_CHAR(SYSDATE, 'YYYYMMDD');

    -- 查询当天的最大流水号
    SELECT NVL(MAX(TO_NUMBER(SUBSTR(warehouse, 9, 4))), 0) + 1
      INTO v_seq
      FROM warehouse
     WHERE SUBSTR(warehouse, 1, 8) = v_date_str;

    -- 拼接编码 = 日期 + 流水号
    p_bill_no := v_date_str || LPAD(v_seq, 4, '0');

    SELECT creator INTO per from project_table where phid = p_phid;
    insert_phid := seq_auto_phid_ck.nextval;
    INSERT INTO
        warehouse (
            product_category,   -- 01
            phid,               -- 02
            warehouse,          -- 03
            whname,             -- 04
            descript,           -- 05
            fwhouse,            -- 06
            whtype,             -- 07
            invwhouse,          -- 08
            whstatus,           -- 09
            isautowhlo,         -- 10
            addr,               -- 11
            manapsn,            -- 12
            tel,                -- 13
            ivtype,             -- 14
            ocode,              -- 15
            isconsign,          -- 16
            verdtm,             -- 17
            statorg,            -- 18
            finorg,             -- 19
            compno,             -- 20
            isonway,            -- 21
            wminus,             -- 22
            wbatchm,            -- 23
            issample,           -- 24
            samplewhouse,       -- 25
            ina_proflg,         -- 26
            isfreeze,           -- 27
            trusteesupply,      -- 28
            ina_init,           -- 29
            ina_initdate,       -- 30
            warehousekeep,      -- 31
            def_int2,           -- 32
            def_int3,           -- 33
            isinsulatewarehouse,-- 34
            isclose,            -- 35
            postflg,            -- 36
            accountflg,         -- 37
            reservewhouse,      -- 38
            isestop,            -- 39
            iswlb,              -- 40
            nation,             -- 41
            province,           -- 42
            city,               -- 43
            county,             -- 44
            shipcomp,           -- 45
            zipcode,            -- 46
            isdealers,          -- 47
            jt_flg,             -- 48
            isequipmentwh,      -- 49
            privcomp,           -- 50
            issubwh,            -- 51
            commissionwh,       -- 52
            nullify,            -- 53
            cur_orgid,          -- 54
            creator,            -- 55
            editor,             -- 56
            ng_insert_dt,       -- 57
            ng_update_dt,       -- 58
            ng_record_ver,      -- 59
            def_int1,           -- 60
            def_num1,           -- 61
            def_date1,          -- 62
            def_str2,           -- 63
            def_str3,           -- 64
            phid_manapsn,       -- 65
            phid_invwhouse,     -- 66
            def_str1,           -- 67
            i6p_flg,            -- 68
            lat,                -- 69
            lng,                -- 70
            phid_org,           -- 71
            ng_phid_cu,         -- 72
            ng_phid_bp,         -- 73
            ng_phid_org,        -- 74
            ng_phid_ui_scheme,  -- 75
            ng_phid_original,   -- 76
            ng_orgid_original,  -- 77
            ng_sv_search_key,   -- 78
            ng_sd_search_key,   -- 79
            ng_share_sign,      -- 80
            isdefault,          -- 81
            freeze_flg,         -- 82
            phid_statistics_org,-- 83
            phid_financial_org, -- 84
            phid_ivtype,        -- 85
            warehousekeeper,    -- 86
            phid_reservewhouse, -- 87
            phid_commissionwh,  -- 88
            phid_samplewhouse,  -- 89
            consign_type,       -- 90
            phid_account_close, -- 91
            account_close_dt,   -- 92
            ffid,               -- 93
            init_confirm_flag,  -- 94
            phid_init_confirm,  -- 95
            init_confirm_dt,    -- 96
            enable_dt,          -- 97
            enable_accper,      -- 98
            enable_year,        -- 99
            upper_limits,       -- 100
            lower_limits        -- 101

        )
    SELECT
        
            'i8',                       -- 01
            insert_phid,   -- 02
            p_bill_no,                  -- 03
            project.ab || '仓库',       -- 04
            '',                         -- 05
            '',                         -- 06
            2,                          -- 07
            '',                         -- 08
            0,                          -- 09
            0,                          -- 10
            '',                         -- 11
            '',                         -- 12
            '',                         -- 13
            '',                         -- 14
            '',                         -- 15
            0,                          -- 16
            null,                       -- 17
            '',                         -- 18
            '',                         -- 19
            '',                         -- 20
            0,                          -- 21
            0,                          -- 22
            0,                          -- 23
            '',                         -- 24
            '',                         -- 25
            0,                          -- 26
            0,                          -- 27
            '',                         -- 28
            0,                          -- 29
            null,                       -- 30
            0.00000000,                 -- 31
            1,                          -- 32
            0,                          -- 33
            0,                          -- 34
            0,                          -- 35
            '0',                        -- 36
            0,                          -- 37
            '',                         -- 38
            0,                          -- 39
            '',                         -- 40
            '',                         -- 41
            '',                         -- 42
            '',                         -- 43
            '',                         -- 44
            '',                         -- 45
            '',                         -- 46
            0,                          -- 47
            '0',                        -- 48
            0,                          -- 49
            '',                         -- 50
            0,                          -- 51
            '',                         -- 52
            '0',                        -- 53
            1,                          -- 54
            project.creator,            -- 55
            project.creator,            -- 56
            SYSDATE,                    -- 57
            SYSDATE,                    -- 58
            2,                          -- 59
            0,                          -- 60
            0.00000000,                 -- 61
            null,                       -- 62
            '',                         -- 63
            '',                         -- 64
            0,                          -- 65
            0,                          -- 66
            '0',                        -- 67
            0,                          -- 68
            '',                         -- 69
            '',                         -- 70
            1,                          -- 71
            1,                          -- 72
            0,                          -- 73
            1,                          -- 74
            0,                          -- 75
            0,                          -- 76
            0,                          -- 77
            '10',                       -- 78
            '1',                        -- 79
            5,                          -- 80
            null,                       -- 81
            '0',                        -- 82
            null,                       -- 83
            null,                       -- 84
            null,                       -- 85
            null,                       -- 86
            null,                       -- 87
            null,                       -- 88
            null,                       -- 89
            null,                       -- 90
            null,                       -- 91
            null,                       -- 92
            0,                          -- 93
            null,                       -- 94
            null,                       -- 95
            null,                       -- 96
            null,                       -- 97
            null,                       -- 98
            null,                       -- 99
            null,                       -- 100
            null                        -- 101

         from project_table project where phid = p_phid;

         INSERT INTO
            kc_accper_warehouse (
                product_category,
                phid,
                uyear,
                phid_warehouse,
                accper,
                begindt,
                enddt,
                proflg,
                endflg,
                def_int1,
                def_int2,
                cur_accper,
                creator,
                cur_orgid,
                editor,
                ng_insert_dt,
                ng_record_ver,
                ng_update_dt,
                ng_phid_cu,
                ng_phid_bp,
                ng_phid_org,
                ng_phid_ui_scheme,
                ng_phid_original,
                ng_orgid_original,
                ng_sv_search_key,
                ng_sd_search_key,
                ng_share_sign,
                ng_write_source
            )
        VALUES
            (
                'i8',
                seq_auto_phid_ckhsq.nextval,
                EXTRACT(YEAR FROM CURRENT_DATE),
                insert_phid,
                EXTRACT(MONTH FROM CURRENT_DATE),
                TRUNC(CURRENT_DATE, 'MM'),
                LAST_DAY(CURRENT_DATE),
                0,
                0,
                0,
                0,
                1,
                per,
                1,
                per,
                SYSDATE,
                1,
                SYSDATE,
                1,
                0,
                1,
                0,
                0,
                0,
                '10',
                '1',
                5,
                0
            );
            -- 提交一次
commit;

--  -- 1️⃣ 初始化 POST 请求
--     req := UTL_HTTP.BEGIN_REQUEST(
--               url    => 'http://10.52.119.173:30847/api/KernelSessionNew',
--               method => 'POST',
--               http_version => 'HTTP/1.1');

--     -- 2️⃣ 设置请求头，声明是 JSON
--     UTL_HTTP.SET_HEADER(req, 'Content-Type', 'application/json; charset=UTF-8');

--     -- 3️⃣ 写入 JSON 字符串
--     UTL_HTTP.WRITE_TEXT(req, '{"ucode":"0001","loginid":"20250926-0001","pwd":"U0Q2akAyMDI2",""ocode":"001"}');

--     -- 4️⃣ 获取响应
--     resp := UTL_HTTP.GET_RESPONSE(req);

--     -- 5️⃣ 读取响应内容
--     BEGIN
--         LOOP
--             UTL_HTTP.READ_LINE(resp, v_line, TRUE);
--             v_text := v_text || v_line;
--         END LOOP;
--     EXCEPTION
--         WHEN UTL_HTTP.END_OF_BODY THEN
--             NULL; -- 读取完成
--     END;

--     -- 6️⃣ 结束响应
--     UTL_HTTP.END_RESPONSE(resp);

--     -- 7️⃣ 打印响应结果
--     DBMS_OUTPUT.PUT_LINE('响应内容: ' || v_text);

EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE ('插入失败，错误信息：' || SQLERRM);

RAISE;

ROLLBACK;

END project_auto_to_ck;